package com.insight.common.exception;

/**
 * T022: 资源重复异常
 * 当尝试创建已存在的资源时抛出（例如：数据源名称重复）
 */
public class DuplicateResourceException extends RuntimeException {

    /**
     * 资源类型
     */
    private final String resourceType;

    /**
     * 冲突的字段名
     */
    private final String fieldName;

    /**
     * 冲突的字段值
     */
    private final Object fieldValue;

    public DuplicateResourceException(String resourceType, String fieldName, Object fieldValue) {
        super(String.format("%s with %s '%s' already exists", resourceType, fieldName, fieldValue));
        this.resourceType = resourceType;
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
    }

    public DuplicateResourceException(String message) {
        super(message);
        this.resourceType = null;
        this.fieldName = null;
        this.fieldValue = null;
    }

    public DuplicateResourceException(String resourceType, String fieldName, Object fieldValue, Throwable cause) {
        super(String.format("%s with %s '%s' already exists", resourceType, fieldName, fieldValue), cause);
        this.resourceType = resourceType;
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
    }

    public String getResourceType() {
        return resourceType;
    }

    public String getFieldName() {
        return fieldName;
    }

    public Object getFieldValue() {
        return fieldValue;
    }
}
